package routinecontroller

import (
	"agent-list---web-development/list/common"
	"agent-list---web-development/list/model"
	"agent-list---web-development/list/response"

	//"fmt"
	"net/http"
	"time"

	//"strconv"

	"github.com/gin-gonic/gin"
)

// 更新打卡事项
func ModifyRoutine(c *gin.Context) {
	DB := common.GetDB()
	title := c.PostForm("title")
	account := c.PostForm("account")
	date := time.Now()
	day := date.Day()
	month := date.Month()
	time := 0
	//数据验证
	var user = model.User{}
	DB.Where("account = ?", account).First(&user)
	// 数据验证
	if user.Id == 0 {
		response.Response(c, http.StatusUnprocessableEntity, 422, nil, "该用户不存在！")
		return
	}

	// 获取对应的用户的数据库
	var routine = model.Routine{}
	DB.Where("useraccount=? and title=?", account, title).First(&routine)

	//如果未找到打卡
	if routine.Id == 0 {
		response.Response(c, http.StatusUnprocessableEntity, 422, nil, "未找到该打卡项目！")
		return
	}
	smonth := routine.STime.Month()
	sday := routine.STime.Day()
	if month != smonth {
		time = day + 30 - sday
	} else {
		time = day - sday
	}
	/*num, err := strconv.Atoi(time)
	if err != nil {
		fmt.Println("转换失败:", err)
		return
	}*/

	routine.Reconding[time] = 1

	// 添加到用户数据库的打卡记录中
	DB.Exec("UPDATE routines SET reconding=? WHERE title=? AND useraccount=?", routine.Reconding, title, account)

	// 上传结果
	response.Success(c, gin.H{"reconding": routine.Reconding}, "修改打卡项成功！")
}
